﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using LinFu.IoC.Configuration;
using LinFu.IoC.Interfaces;
using LinFu.IoC;

namespace CoreEx.Database.Client.Implementation
{
    [Factory(typeof(IDbCommand))]
    public class CommandFactory : IFactory<IDbCommand>
    {
        public IDbCommand CreateInstance(IFactoryRequest request)
        {            
            //Get the DbProviderFactory that corresponds to the provider name;
            var dbProviderFactory = request.Container.GetService<DbProviderFactory>(request.ServiceName);

            //Create the IDbCommand instance 
            var command = dbProviderFactory.CreateCommand();
            command.CommandType = CommandType.Text;

            return request.Container.GetService<IProxyFactory<IDbCommand>>().CreateProxy(command);
        }
    }
}
